var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { remark } from 'remark';
import { visit } from 'unist-util-visit';
import { coreRemarkMdxPlugins } from '../../remark.js';
import { isCode, convertCode } from './code.js';
import { isImage, convertImage } from './image.js';
import { isList, convertList } from './list.js';
import { isTable, convertTable } from './table.js';
import { willBeMdxJsxFlowElement, willBeMdxJsxTextElement, convertToMdxJsxTextElement, convertToMdxJsxFlowElementBase, convertToMdxJsxTextElementBase, convertToMdxJsxTextElementWithFirstTextChildValue, } from './utils.js';
/**
 *
 * @param content mdx content w/ basic md
 * @returns mdx content w/ basic md converted to html
 */
export const mdxMdToHtml = (content) => __awaiter(void 0, void 0, void 0, function* () {
    const convertedContent = yield remark()
        .use(coreRemarkMdxPlugins)
        .use(remarkMdxMdToHtml)
        .process(content);
    return String(convertedContent);
});
// TODO: Open source as remark plugin
/**
 * Remark plugin that converts mdx md to html
 */
export const remarkMdxMdToHtml = () => (tree) => {
    visit(tree, (node) => {
        if (isLink(node)) {
            node.type = 'mdxJsxTextElement';
            node.name = 'a';
            node.attributes = [
                {
                    type: 'mdxJsxAttribute',
                    name: 'href',
                    value: node.url,
                },
            ];
        }
        else if (willBeMdxJsxTextElement(node, node.type === 'inlineCode')) {
            convertToMdxJsxTextElement(node, 'code', [{ type: 'text', value: node.value }]);
        }
        else if (isHeading(node)) {
            convertToMdxJsxTextElementWithFirstTextChildValue(node, `h${node.depth}`);
        }
        else if (willBeMdxJsxTextElement(node, node.type === 'strong')) {
            convertToMdxJsxTextElementWithFirstTextChildValue(node, 'strong');
        }
        else if (willBeMdxJsxTextElement(node, node.type === 'emphasis')) {
            convertToMdxJsxTextElementWithFirstTextChildValue(node, 'em');
        }
        else if (willBeMdxJsxTextElement(node, node.type === 'blockquote')) {
            convertToMdxJsxTextElementBase(node, 'blockquote');
        }
        else if (willBeMdxJsxTextElement(node, node.type === 'paragraph')) {
            convertToMdxJsxTextElementBase(node, 'p');
        }
        else if (isList(node)) {
            convertList(node);
        }
        else if (isImage(node)) {
            convertImage(node);
        }
        else if (willBeMdxJsxFlowElement(node, node.type === 'thematicBreak')) {
            convertToMdxJsxFlowElementBase(node, 'hr');
        }
        else if (isCode(node)) {
            convertCode(node);
        }
        else if (isTable(node)) {
            convertTable(node);
        }
    });
};
const isHeading = (node) => node.type === 'heading';
const isLink = (node) => node.type === 'link';
